const mongoose = require("mongoose");
var Ristory = mongoose.model("ristory");


exports.addRistory = function(req,res){
	var ristory = new Ristory();
	ristory.rname = req.body.rname;
	ristory.address = req.body.address;
	ristory.save(function(err,ristory){
		if(!err){
			res.json(ristory);
		}else{
			res.json({error: "can't add ristory"});
		}
	})
}
exports.getRistories = function(req,res){
	Ristory.find({}).populate("product").exec(function(err,ristories){
		if(!err){
			res.json(ristories);
		}else{
			res.json({error: "can't find ristories"});
		}
	})
}

exports.deleteRistory = function(req,res){
	Ristory.findOne({_id:req.params.id},function(err,ristory){
		if(!err){
			ristory.remove(function(err){
				if(!err){
					res.json({});
				}else{
					res.json({error:"can't remove ristory"});
				}
			})
		}else{
			res.json({error:"can't find ristory by id = "+req.params.id });
		}
	})
}

